package com.itheima.case1;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class case4Runnable implements Runnable{
    static Integer restAmount = 100;

    static Integer restPeopleNum = 3;

    static Lock lock = new ReentrantLock();
    Random random = new Random();


    @Override
    public void run() {
//        while(true){
            lock.lock();
            try{
                if(restPeopleNum > 1) {
                    int amount = random.nextInt(restAmount / restPeopleNum * 2 - 1) + 1;
                    restAmount = restAmount - amount;
                    restPeopleNum--;
                    System.out.println(Thread.currentThread().getName() + "抢到了" + amount + "元");
                }else if(restPeopleNum == 1){
                    restPeopleNum--;
                    System.out.println(Thread.currentThread().getName() + "抢到了" + restAmount + "元");
                    restAmount = restAmount - restAmount;
                }else{
//                    break;
                    System.out.println(Thread.currentThread().getName() + "没有抢到");
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
//        }

    }
}
